//
//  WSDatePickerView.m
//  BaseProject
//
//  Created by karong on 2023/7/21.
//

#import "WSDatePickerView.h"

@interface WSDatePickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSDateFormatter * dateFormat;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) NSInteger yearWidth;
@property (nonatomic, assign) NSInteger monthWidth;
@property (nonatomic, assign) NSInteger dayWidth;

@end

@implementation WSDatePickerView

- (void)initPickView {
    [super initPickView];
    //设置数据
    _minShowYear = 1940;//最小年份
    NSCalendar *calendar = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* date = [NSDate date];
    // 指定获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [calendar components:unitFlags fromDate:date];
    _yearSum = comp.year-_minShowYear+1;
    _currentYear = comp.year;
    _currentMonth = comp.month;
    _currentDay = comp.day;
    _selectYear  = comp.year;
    _selectMonth = comp.month;
    _selectDay = comp.day;
    _defaultYear = comp.year;
    _defaultMonth = comp.month;
    _defaultDay = comp.day;
    _yearWidth = 82;
    _monthWidth = 60;
    _dayWidth = 60;
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
}

- (void)setDefaultSelectDay:(NSString *)day dateFormat:(NSString *)dateFormat  {
    self.count = 3;
    [self setDefaultSelect:day dateFormat:dateFormat];
}

- (void)setDefaultSelectMonth:(NSString *)day dateFormat:(NSString *)dateFormat {
    self.count = 2;
    [self setDefaultSelect:day dateFormat:dateFormat];
    
}

- (void)setDefaultSelectYear:(NSString *)day dateFormat:(NSString *)dateFormat {
    self.count = 1;
    [self setDefaultSelect:day dateFormat:dateFormat];
}

- (void)setDefaultSelect:(NSString *)day dateFormat:(NSString *)dateFormat {
    [self.dateFormat setDateFormat:dateFormat];
    NSDate * date = [self.dateFormat dateFromString:day];
    if (date.year != 0) {
     _defaultYear = date.year;
    }
    if (date.month != 0) {
        _defaultMonth = date.month;
    }
    if (date.day != 0) {
         _defaultDay = date.day;
    }
    if (date.year == -1) {
        _defaultYear =_currentYear+1;
        _defaultMonth = 1;
        _defaultDay = 1;
    }
    [self.pickerView selectRow:(_defaultYear - _minShowYear) inComponent:0 animated:NO];
    if (self.count > 1) {
        [self.pickerView selectRow:(_defaultMonth - 1) inComponent:1 animated:NO];
        [self.pickerView reloadComponent:1];
    }
    if (self.count > 2) {
        [self.pickerView selectRow:(_defaultDay-1) inComponent:2 animated:NO];
        [self.pickerView reloadComponent:2];
    }
    [self refreshPickViewData];
    [self addUnit];
}

- (void)addUnit {
    NSArray * array = @[@"年", @"月", @"日"];
    NSArray * widthArray = @[@(self.yearWidth), @(self.monthWidth), @(self.dayWidth)];
    NSMutableArray * xArray = [NSMutableArray new];
    NSInteger totalWidth = 0;
    for (int i = 0; i < self.count; i++) {
        NSInteger width = [widthArray[i] integerValue];
        totalWidth += width;
        [xArray addObject:@(totalWidth)];
    }
    for (int i = 0; i < self.count; i++) {
        [self addLabel:array[i] totalWidth:totalWidth x:[xArray[i] integerValue]];
    }
}

- (void)addLabel:(NSString *)unit totalWidth:(NSInteger)totalWidth x:(NSInteger)x {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = UIColor.font_white_ffffff;
    label.text = unit;
    CGFloat left = (self.pickerView.width - totalWidth) / 2.0f;
    CGPoint center = self.pickerView.center;
    center.x = left + x;
    center.y -= 48;
    label.center = center;
    [self.pickerView addSubview:label];
}
//返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    //每一行的高度
    return 40;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    NSInteger width = 0;
    switch (component) {
        case 0:
            width = self.yearWidth;
            break;
        case 1:
            width = self.monthWidth;
            break;
        case 2:
            width = self.dayWidth;
            break;
        default:
            break;
    }
    return width;
}

//返回行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.yearSum;
    }
    else if(component == 1) {
            return 12;
    }
    else if(component == 2) {
        NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.minShowYear;
        NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
        return  [self getDaysWithYear:yearSelected month:monthSelected];
    }
    else {
        return 0;
    }
}

- (NSInteger)getDaysWithYear:(NSInteger)year month:(NSInteger)month {
    switch (month) {
        case 1:
            return 31;
            break;
        case 2:
            //分为闰年和平年
            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
                return 29;
            }else{
                return 28;
            }
            break;
        case 3:
            return 31;
            break;
        case 4:
            return 30;
            break;
        case 5:
            return 31;
            break;
        case 6:
            return 30;
            break;
        case 7:
            return 31;
            break;
        case 8:
            return 31;
            break;
        case 9:
            return 30;
            break;
        case 10:
            return 31;
            break;
        case 11:
            return 30;
            break;
        case 12:
            return 31;
            break;
        default:
            return 0;
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [pickerView reloadComponent:component];
    switch (component) {
        case 0:
            if (self.count > 2) {
                [pickerView reloadComponent:2];
            }
            break;
        case 2:
            if (self.count > 2) {
                [pickerView reloadComponent:2];
            }
        default:
            break;
    }
    [self refreshPickViewData];
}
//更新数据
- (void)refreshPickViewData {
    self.selectYear = [self.pickerView selectedRowInComponent:0];
    if (self.count > 1) {
        self.selectMonth = [self.pickerView selectedRowInComponent:1];
    }
    if (self.count > 2) {
        self.selectDay = [self.pickerView selectedRowInComponent:2];
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UIColor * color = UIColor.font_white_ffffff;
    NSString *text;
    if (component == 0) {
        text = [NSString stringWithFormat:@"%zd", row + _minShowYear];
        color = (row == self.selectYear)?UIColor.font_yellow_FCB625:UIColor.font_white_ffffff;
    }
    else if (component == 1) {
        text = [NSString stringWithFormat:@"%zd", row + 1];
        color = (row == self.selectMonth)?UIColor.font_yellow_FCB625:UIColor.font_white_ffffff;
    }
    else if (component == 2) {
        text = [NSString stringWithFormat:@"%zd", row + 1];
        color = (row == self.selectDay)?UIColor.font_yellow_FCB625:UIColor.font_white_ffffff;
    }
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:24];
    label.textColor = color;
    label.text = text;
    return label;
}

- (void)clickConfirmButton {
    if ([self.delegate respondsToSelector:@selector(pickerDateView:selectYear:selectMonth:selectDay:)]) {
        [self.delegate pickerDateView:self selectYear:self.selectYear + self.minShowYear selectMonth:self.selectMonth + 1 selectDay:self.selectDay + 1];
    }
    //为了保留dismiss操作
    [super clickConfirmButton];
    
}

- (NSDateFormatter *)dateFormat {
    if (!_dateFormat) {
        _dateFormat = [[NSDateFormatter alloc]init];
    }
    return _dateFormat;
}

@end
